package nio.file;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class FilesTest2 {

	public static void main(String[] args) {
		
		Charset cs = Charset.forName( "UTF-8" );
		
		Path p = Path.of( "src/nio/file/FilesTest2.java" );
		
		if( Files.exists( p ) ) {
			
			BufferedReader br = null ;
			
			try {
				// 创建一个带有缓冲功能的字符输入流，同时制定 charset
				br = Files.newBufferedReader( p , cs );
				
				String s ;
				while( ( s = br.readLine() ) != null ) {
					System.out.println( s );
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if( br != null ) {
					try {
						br.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			
		}
		
		System.out.println( "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" );
		
		try {
			// 读取 p 所对应的文本问价那种的所有行，每行作为一个元素保存到 List 集合中
			List<String> lines = Files.readAllLines( p , cs );
			for( String line : lines ) {
				System.out.println( line );
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
